package com.xmcowell.sportpacemaster.views;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/1.
 */

public class MainLeftView extends LinearLayout {
    private  Integer[] images;
    //手指按下的点为(x1, y1)手指离开屏幕的点为(x2, y2)
    private float x1 = 0;
    private float x2 = 0;
    private float y1 = 0;
    private float y2 = 0;

    private int i = 0;//当前显示的是第几张
    private ImageView imageView;

    public MainLeftView(Context context) {
       this(context,null);
    }

    public MainLeftView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MainLeftView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initUI(context);
    }

    public void initUI(Context context){
        imageView = new ImageView(context);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        imageView.setLayoutParams(params);
        this.addView(imageView);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //继承了Activity的onTouchEvent方法，直接监听点击事件
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //当手指按下的时候
                x1 = event.getX();
                y1 = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                //当手指移动的时候
                x2 = event.getX();
                y2 = event.getY();
                if (x1 - x2 > 100) {
                    if (i == 7){
                        i = 0;
                    }else{
                        i++;
                    }
                    imageView.setImageResource(images[i]);
//                Toast.makeText(MainActivity.this, "向左滑", Toast.LENGTH_SHORT).show();
                } else if (x2 - x1 > 100) {
                    if (i == 0){
                        i = 7;
                    }else{
                        i--;
                    }
                    imageView.setImageResource(images[i]);
//                Toast.makeText(MainActivity.this, "向右滑", Toast.LENGTH_SHORT).show();
                }
                break;
        }
        return true;
    }

    public void setImages(Integer[] images){
        this.images = images;
        if (imageView!=null){
            imageView.setImageResource(images[0]);
            }
    }

}
